<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Crm\Controller;

use PHPExcel;
use PHPExcel_IOFactory;
use Zend\View\Model\ViewModel;
use Crm\Common\CommonHelper;

class rechargeController extends AbstractController
{
    /*
     * 储值卡列表
     */
    public function indexAction()
    {
        return new ViewModel();
    }
    
    /*
     * 储值卡列表
     */
    public function ajaxRechargeTableAction()
    {
        
        $post = $this->request->getPost()->toArray();
        
        $paginator = $this->getRechargeCardTable()->getRechargeCardList($post);
        $result = array(
            'paginator' => $paginator,
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
              ->setTerminal(true);

        return $viewModel;
    }
    public function deleteAction()
    {
        $request = $this->getRequest();
        $rc_id = $request->getPost('rc_id');
        try{
            $this->getRechargeCardTable()->deleteItem($rc_id);
            $response=array(
                'result' => 'true',
                'reason' => '删除成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $response=array(
                'result' => 'false',
                'reason' => $e,
            );
            die(json_encode($response));
        }
    }
    /*
     * 储值卡详情
     */
    public function rechargeDetailAction()
    {
        $params=$this->getEvent()->getRouteMatch()->getParams();
        $rc_id=isset($params['id'])?$params['id']:'';
        $rechargeCard =array();
        //$paginator =array();
        if($rc_id != ''){
            //$paginator = $this->getRechargeTable()->getRechargeList(array('rc_id'=>$rc_id));
            $rechargeCard = $this->getRechargeCardTable()->getByRechargeId($rc_id);
        }
        
        return new ViewModel(array(
            'rc_id' =>$rc_id,
            'rechargeCard' => $rechargeCard,
            //'paginator' => $paginator,
            
        ));
    }
    
    /*
     * 储值卡详情列表
     */
    public function ajaxRechargeListAction()
    {
    
        $post = $this->request->getPost()->toArray();
    
        $paginator = $this->getRechargeTable()->getRechargeList($post);
        $result = array(
            'paginator' => $paginator,
        );
    
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
        ->setTerminal(true);
    
        return $viewModel;
    }
    
    public function saveAction()
    {
        $rechargeCartTable = $this->getRechargeCardTable();
        $data = $this->request->getPost()->toArray();
        try{
            if($data['rc_id'] != ''){
                $rechargeCartTable->updateItem($data['rc_id'],$data);
            }else{
                try{
                    $rechargeCartTable->beginTransaction();
                    $data['rc_id'] = $rechargeCartTable->addItem($data);
                    $count = 0;
                    
                    while ($count != $data['num']){
                        $uid = uniqid ("", true );
                        $guid = strtoupper( md5($uid.$data['start_time'].$data['name']) );
                        $code = substr($guid, 0,18);
                        $result = $this->getRechargeTable()->getRechargeByCode($code);
                        if($result === false){
                            $recharge = array(
                                'rc_id'=>$data['rc_id'],
                                'code'=>$code,
                            );
                            $this->getRechargeTable()->addItem($recharge);
                            $count += 1;
                        }
                    }
                    $rechargeCartTable->commit();
                }catch(\Exception $e)
                {
                    die($e);
                    $rechargeCartTable->rollBack();
                    $this->flashMessenger()->addErrorMessage("生成失败！");
                    return $this->redirect()->toRoute('crm/recharge',array('action'=>'recharge-detail'));
                }
            }
            $this->flashMessenger()->addSuccessMessage("保存成功！");
            return $this->redirect()->toRoute('crm/recharge',array('action'=>'recharge-detail','id'=>$data['rc_id']));
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("修改失败！");
            return $this->redirect()->toRoute('crm/recharge',array('action'=>'order-detail','id'=>$data['rc_id']));
        }
    }
    public function excelDetailAction()
    {
        $params=$this->getEvent()->getRouteMatch()->getParams();
        $rc_id=isset($params['id'])?$params['id']:'';
        $outExcelList = array();
        $outExcelList = $this->getRechargeTable()->getOutExcelList(array('rc_id'=>$rc_id));
        
        $objPHPExcel = new PHPExcel();
        //设置title
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '储值卡类型ID')
        ->setCellValue('B1', '储值卡名称')
        ->setCellValue('C1', '储值卡数量')
        ->setCellValue('D1', '金额')
        ->setCellValue('E1', '开始日期')
        ->setCellValue('F1', '截止日期')
        ->setCellValue('G1', '储值卡备注')
        ->setCellValue('H1', '储值卡ID')
        ->setCellValue('I1', '储值卡编码')
        ->setCellValue('J1', '绑定用户ID')
        ->setCellValue('K1', '绑定用户姓名')
        ->setCellValue('L1', '使用状态');
        
        //详细数据
        foreach ($outExcelList as $key=>$recharge){
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.($key + 2), $recharge['rc_id'])
            ->setCellValue('B'.($key + 2), $recharge['rc_name'])
            ->setCellValue('C'.($key + 2), $recharge['rc_num'])
            ->setCellValue('D'.($key + 2), $recharge['rc_amount'])
            ->setCellValue('E'.($key + 2), $recharge['rc_start_time'])
            ->setCellValue('F'.($key + 2), $recharge['rc_end_time'])
            ->setCellValue('G'.($key + 2), $recharge['rc_remark'])
            ->setCellValue('H'.($key + 2), $recharge['id'])
            ->setCellValue('I'.($key + 2), $recharge['code'])
            ->setCellValue('J'.($key + 2), $recharge['user_id'])
            ->setCellValue('K'.($key + 2), $recharge['user_name'])
            ->setCellValue('L'.($key + 2), $recharge['has_use']=='0'?'未使用':'已使用');
        }
        
        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle($outExcelList[0]['rc_name']);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 生成excel文件 Excel2007
        // $objWriter->save($dir."/export_1.xls");//保存文件
        CommonHelper::browserExport('Excel5', $outExcelList[0]['rc_name'].'_'.date('YmdHis').'.xls'); // 输出到浏览器
        $objWriter->save("php://output");
        exit();
    }
    public function excelRechargeAction()
    {
        $outExcelList = array();
        $outExcelList = $this->getRechargeCardTable()->getOutExcelList();
        
        $objPHPExcel = new PHPExcel();
        //设置title
        $objPHPExcel->setActiveSheetIndex(0)
        ->setCellValue('A1', '储值卡类型ID')
        ->setCellValue('B1', '储值卡名称')
        ->setCellValue('C1', '储值卡数量')
        ->setCellValue('D1', '金额')
        ->setCellValue('E1', '开始日期')
        ->setCellValue('F1', '截止日期')
        ->setCellValue('G1', '储值卡备注');
        
        //详细数据
        foreach ($outExcelList as $key=>$recharge){
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValue('A'.($key + 2), $recharge['rc_id'])
            ->setCellValue('B'.($key + 2), $recharge['rc_name'])
            ->setCellValue('C'.($key + 2), $recharge['rc_num'])
            ->setCellValue('D'.($key + 2), $recharge['rc_amount'])
            ->setCellValue('E'.($key + 2), $recharge['rc_start_time'])
            ->setCellValue('F'.($key + 2), $recharge['rc_end_time'])
            ->setCellValue('G'.($key + 2), $recharge['rc_remark']);
        }
        
        // Rename worksheet
        $objPHPExcel->getActiveSheet()->setTitle($outExcelList[0]['rc_name']);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); // 生成excel文件 Excel2007
        // $objWriter->save($dir."/export_1.xls");//保存文件
        CommonHelper::browserExport('Excel5', $outExcelList[0]['rc_name'].'_'.date('YmdHis').'.xls'); // 输出到浏览器
        $objWriter->save("php://output");
        exit();
    }
}
